package cn.bugstack.mybatis.executor.statement;



import cn.bugstack.mybatis.executor.Executor;
import cn.bugstack.mybatis.executor.resultset.ResultSetHandler;
import cn.bugstack.mybatis.mapping.BoundSql;
import cn.bugstack.mybatis.mapping.MappedStatement;
import cn.bugstack.mybatis.session.Configuration;
import cn.bugstack.mybatis.session.ResultHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public abstract class BaseStatementHandler implements StatementHandler{

    protected final Configuration configuration; // 配置

    protected final Executor executor;

    protected final MappedStatement mappedStatement;

    protected final Object parameterObject;

    protected final ResultSetHandler resultSetHandler;

    protected final BoundSql boundSql;



    public BaseStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, ResultHandler resultHandler, BoundSql boundSql) {
       // this.configuration = configuration;
        this.configuration = mappedStatement.getConfiguration();  // 避免获取到其他的连接
        this.executor = executor;
        this.mappedStatement = mappedStatement;
        this.parameterObject = parameterObject;
        //this.resultHandler = resultHandler;
        this.boundSql = boundSql;
        // 结果处理器 为啥需要通过配置类获取？ 20250320
        this.resultSetHandler = configuration.newResultSetHandler(executor, mappedStatement,  boundSql);
    }

    @Override
    public Statement prepare(Connection connection) throws SQLException {
        Statement statement = null;
        try {
            statement = instantiateStatement(connection);
            statement.setQueryTimeout(350);
            statement.setFetchSize(10000);
            return  statement;
        }catch(Exception e){
            throw new RuntimeException("Error preparing statement.  Cause: " + e, e);
        }
    }

    protected abstract Statement instantiateStatement(Connection connection) throws SQLException;


}
